﻿package org.hyy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hyy.dao.UserDAO;

public class Login extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*
		 * 获取参数
		 */
		String username = request.getParameter("username");
		String password	= request.getParameter("passwd");
		String verifycode = request.getParameter("verifycode");
		HttpSession session = request.getSession();
		String code	= (String)session.getAttribute("rand");
		//验证码错误
		if(!code.equals(verifycode)){
			request.setAttribute("err", "验证码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else{
			UserDAO userDAO = new UserDAO();
			boolean flag = userDAO.login(username, password);
			System.out.print(flag);
			if(flag){
				session.setAttribute("user", username);
				session.setAttribute("show", "log");
				response.sendRedirect("success.jsp");
			}else{
				response.sendRedirect("loginerr.jsp");
			}
		}
	}

}
